const Controller = require('egg').Controller

class QuoteController extends Controller {
  async getQuote() {
    const {ctx} = this
    let quoteList = await ctx.service.quote.getQuoteList()
    if(quoteList) {
      ctx.body = {
        code: 20000,
        data: quoteList
      }
    }else{
      ctx.body = {
        code: 50000,
        data: "服务器异常"
      }
    }
  }

  async index() {
    const {ctx} = this
    let quotes = await ctx.service.quote.getQuotes()
    if(quotes) {
      ctx.body = {
        code:20000,
        data:quotes
      }
    }else{
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

  async create() {
    const { ctx } = this
    let body = ctx.request.body.data
    // console.log(body);
    let res = await ctx.service.quote.addQuote(body)
    if(res) {
      ctx.body = {
        code:20000,
        data:'添加成功'
      }
    }else {
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

  async destroy() {
    const { ctx } = this
    let id = ctx.params.id
    // console.log(id);
    let res = await ctx.service.quote.deleteQuote(id)
    if(res) {
      ctx.body = {
        code:20000,
        data:'删除成功'
      }
    }else{
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }
}
module.exports = QuoteController